//
//  PopoverPresentationController.swift
//  微博
//
//  Created by expai on 16/5/6.
//  Copyright © 2016年 guangJun.com.cn. All rights reserved.
//

import UIKit

@available(iOS 8.0, *)

class PopoverPresentationController:UIPresentationController{
    
    //定义属性保存菜单的大小
    var presentFrame = CGRectZero
    
    /**
     初始化方法, 用于创建负责转场动画的对象
     
     :param: presentedViewController  被展现的控制器
     :param: presentingViewController 发起的控制器, Xocde6是nil, Xcode7是野指针
     
     :returns: 负责转场动画的对象
     */
    override init(presentedViewController: UIViewController, presentingViewController: UIViewController) {
        
        super.init(presentedViewController: presentedViewController, presentingViewController: presentingViewController)
        
        print(presentedViewController)
    }
    
    //即将布局转场子视图时调用
    override func containerViewWillLayoutSubviews() {
        //修改弹出视图的大小
        if presentFrame == CGRectZero{
            presentedView()?.frame = CGRect(x: 100, y: 56, width: 200, height: 200)
        }
        else
        {
            presentedView()?.frame = presentFrame
        }
        // 2.在容器视图上添加一个蒙版, 插入到展现视图的下面
        containerView?.insertSubview(coverView, atIndex: 0)
    }
    
    private lazy var coverView:UIView = {
        
        let view = UIView()
        view.backgroundColor = UIColor(white: 0.0, alpha: 0.2)
        view.frame = UIScreen.mainScreen().bounds
        
        //添加监听
        let tap = UITapGestureRecognizer(target: self, action: #selector(close))
        view.addGestureRecognizer(tap)
        return view
    }()
    
    func close(){
        presentedViewController.dismissViewControllerAnimated(true, completion: nil)
    }
}
